import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.net.*;
import java.lang.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class writeToFileClass {
 
  int client_id;
    Socket socket;    
    String folder;
    //String vehi;
    FileOutputStream fout;
    String tempTxt=null;
    StringBuilder line=new StringBuilder();
    //GeoCode geocode = new GeoCode();
    GeoFun geofun = new GeoFun();
    sendAlert sa = new sendAlert();
    
  
  public void makeFolder(String strFilename,String dataTxt,String vehino) throws Exception
  {
    System.out.println("name "+strFilename);
   File fos=new File(strFilename);
   FileWriter fw=new FileWriter(fos,true);
   BufferedWriter bw=new BufferedWriter(fw);
   bw.write(dataTxt);
   bw.close();
  }
   public boolean readFile(String path,String Text,String vehi1) 
   {
 String str1=null;
 try
 {
  FileInputStream fstream = new FileInputStream(path);
  DataInputStream in = new DataInputStream(fstream);
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
   str1= br.readLine();
  in.close();
 }
 catch (Exception e){
   //System.err.println("Error: " + e.getMessage());
   str1="";
 }
  if(str1.indexOf(Text)==-1)
  {
   return true;
  }
  else
  {
   return false;
  }
    }
    
   public void readData(String strText) throws Exception
   {
    String nwDate=null;
    String fTxt = null;
    String subFolder=null;
    String strLine=null;
    
    try
    {
      //System.out.println("Data "+strText);   
       String[] data1=strText.split("\\^");
       
       for(int j1=0;j1<data1.length;j1++)
       {
       
         String[] data2=data1[j1].split("~");
         
     
         String[] data3=data2[1].split(",");
         String vehiStr[] = data3[0].split("\\|");
         String vehi = vehiStr[0];
         String[] date = vehiStr[2].split("(?<=\\G.{2})");
         String[] time = data3[1].split("(?<=\\G.{2})");
         
         String[] newDateTime=geofun.makeDateTime(date[0],date[1],date[2],time[0],time[1],time[2]).split(",");  
         
         String newLat  = "";
         if(Float.parseFloat(data3[2]) != 0.00000)
         {
           String latTxt = data3[2].replace(".","")+data3[3];
           Double latTxt1 = geofun.round(geofun.convertLat(geofun.calLat(latTxt)),8);
           newLat = Double.toString(latTxt1).replace(".","");
         }
         String newLng  = "";
         if(Float.parseFloat(data3[4]) != 0.00000)
         {
         String lngTxt = data3[4].replace(".","")+data3[5];
         Double lngTxt1 = geofun.round(geofun.convertLong(geofun.calLong(lngTxt)),8);         
         newLng = Double.toString(lngTxt1).replace(".","");        
         }
         
         long speed1 = Math.round(Double.parseDouble(data3[6]));
         
         
         String otherParam = "";
         otherParam += "[Distance="+Math.round(Double.parseDouble(data3[9]))+"]";
         
         String IOState1 =  geofun.HexToBinary(data3[10]);
         String[] IOState = IOState1.split("(?<=\\G.{1})");   
     
         //otherParam += " [Ignition="+IOState[7]+"]";
         otherParam += " [1001="+IOState[7]+"]";
         otherParam += " [ExternalPower="+IOState[6]+"]";
         
         for(int i=2; i < 6; i++)
         {
           otherParam += " ["+(i-1)+"="+IOState[i]+"]";
         }
         otherParam += " [NWStatus7="+IOState[1]+"]";
         otherParam += " [NWStatus8="+IOState[0]+"]";        
         
         String IOState2 =  geofun.HexToBinary(data3[11]);
         String[] IOState3 = IOState2.split("(?<=\\G.{1})");   
     
         for(int i=4; i < 8; i++)
         {
           otherParam += " [DOUT"+(i+1)+"="+IOState3[i]+"]";
         }
         otherParam += " [Reserve="+IOState3[3]+"]";
         otherParam += " [CSQ6="+IOState3[2]+"]";
         otherParam += " [CSQ7="+IOState3[1]+"]";
         otherParam += " [CSQ8="+IOState3[0]+"]";                  
         
         String[] NOS_ALT = data3[12].split("(?<=\\G.{1})");        
         int NOS =  geofun.hex2decimal(NOS_ALT[0]);
         
         String ALTData = NOS_ALT[1]+""+NOS_ALT[2]+""+NOS_ALT[3];
         int ALT = geofun.hex2decimal(ALTData);         
         
         String Input1 = geofun.HexToBinary(data3[13]);//data3[13].split("(?<=\\G.{1})");   
         String[] Input2 = Input1.split("(?<=\\G.{1})");   
         String FuelData = "";
         for(int i=8; i < 16; i++)
         {
           FuelData += Input2[i];
         }
         otherParam += " [Fuel="+ geofun.Bin2Decimal(FuelData)+"]";
         String IBVData = "";
         for(int i=2; i < 8; i++)
         {
           IBVData += Input2[i];
         }
         otherParam += " [IBVData="+ geofun.Bin2Decimal(IBVData)+"]";
         otherParam += " [GPSOpen="+ Input2[1]+"]";
         otherParam += " [GPSShort="+ Input2[0]+"]";
         
         otherParam += " [NetOprCode="+ data3[14]+"]";
         otherParam += " [DeltaDist="+ data3[15]+"]";
         
         String[] AnalogData = data3[16].split("\\*");
         String Analog = geofun.HexToBinary(AnalogData[0]);//data3[13].split("(?<=\\G.{1})");   
         String[] Analog1 = Analog.split("(?<=\\G.{1})");   
         String AnaCount2 = "";
         for(int i=20; i < 32; i++)
         {
           AnaCount2 += Analog1[i];
         }
         otherParam += " [AnaCount2="+ geofun.Bin2Decimal(AnaCount2)+"]";
         
         String AnaCount1 = "";
         for(int i=8; i < 20; i++)
         {
           AnaCount1 += Analog1[i];
         }         
         otherParam += " [AnaCount1="+ geofun.Bin2Decimal(AnaCount1)+"]";

         String EBVData = "";
         for(int i=2; i < 7; i++)
         {
           EBVData += Analog1[i];
         }         
         otherParam += " [EBVData="+ geofun.Bin2Decimal(EBVData)+"]";
         otherParam += " [Reserve31="+ Analog1[1]+"]";
         otherParam += " [Reserve32="+ Analog1[0]+"]";
         
         //System.out.println("vehi "+data3[16]+" "+ geofun.HexToBinary(AnalogData[0])+" \n");  
      
         fTxt="@"+vehi+","+newLng+","+newLat+","+speed1+","+data3[8]+","+ALT+","+NOS+","+otherParam+","+newDateTime[0]+","+newDateTime[1]+"#";   
         //System.out.println("ss"+fTxt);
   
         subFolder="/opt/lampp/htdocs/src/data/"+newDateTime[0]+"/";
         //System.out.println("subFolder "+subFolder);
         File sub = new File(subFolder);
         try
         {
           if(sub.mkdirs()){}
           else{}
         }
         catch(Exception e)
         {
           e.printStackTrace();
         }    
     subFolder+=vehi+".txt";
     if(readFile(subFolder,fTxt,vehi))
     {
       //System.out.println("file "+fTxt);
       makeFolder(subFolder,fTxt,vehi);
       sa.sendSMSAlert(fTxt);
     } 
    }
   
    }   
    catch(Exception e)
    {
       System.out.println("Problem finding file");
    }
  }
 

   /*public static void main (String[] args) 
   {
   writeToFileClass wtf= new writeToFileClass();
   try
   {
    //wtf.readData("~AEM|PPD|200507,101536,2836.93139,N,7722.62742,E,036,A,315,234.682,43,E0,A0CB,1000,40410,0.115,1829E80D*05^");
     wtf.readData("~AEMRFID001|PPD|180312,000533,0.00000,N,0.00000,E,000,V,000,19837.828,42,C0,0000,1000,40410,0.000,18000000*40^");
     //wtf.readData("~AEMRFID001|PPD|150312,183708,2822.88745,N,7718.37640,E,001,A,343,19837.475,42,A0,90C6,1000,40410,0.000,18000000*26^~AEMRFID001|PPD|150312,183608,2822.88647,N,7718.37840,E,000,A,020,19837.475,42,A0,A0C6,1000,40410,0.000,18000000*55^");
   }
   catch(Exception e)
   {
    
   }
 }*/
    
}
